package cn.dev33.satoken.oauth2.logic.type;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Handle;
import com.csap.framework.annotation.EnumMessage;
import com.csap.mybatisplus.annotation.EnumValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.collections.MapUtils;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 返回类型
 *
 * @author yangchengfu
 * @dataTime 2021年-08月-03日 13:41:00
 **/
@Getter
@AllArgsConstructor
public enum GrantType {
    //授权类型
    authorization_code("authorization_code", "授权码类型", (i) -> SaOAuth2Handle.token(SaHolder.getRequest(), SaHolder.getResponse(), SaOAuth2Manager.getConfig())),
    refresh_token("refresh_token", "刷新token", (i) -> SaOAuth2Handle.refreshToken(SaHolder.getRequest())),
    password("password", "密码登录", (i) -> SaOAuth2Handle.password(BeanMap.create(i), SaHolder.getRequest(), SaHolder.getResponse(), SaOAuth2Manager.getConfig())),
    sms("sms", "短信登录", (i) -> SaOAuth2Handle.password(BeanMap.create(i), SaHolder.getRequest(), SaHolder.getResponse(), SaOAuth2Manager.getConfig())),
    client_credentials("client_credentials", "应用登录", (i) -> SaOAuth2Handle.clientToken(SaHolder.getRequest(), SaHolder.getResponse(), SaOAuth2Manager.getConfig()));
    @EnumValue
    private final String code;
    @EnumMessage
    private final String name;
    private final Function<Object, Object> cmd;

    @Override
    public String toString() {
        return code;
    }
}
